package ch.usi.inf.pf2.gui.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ch.usi.inf.pf2.editor.MultiplePaste;
import ch.usi.inf.pf2.editor.MultipleTextManagement;
import ch.usi.inf.pf2.editor.Paste;
import ch.usi.inf.pf2.editor.TextManagement;
import ch.usi.inf.pf2.gui.JSpreadSheetGUI;
import ch.usi.inf.pf2.gui.SpreadSheetTableModel;

/**
 * Listener for the menu and button "Paste"
 * 
 * @author Lorenzo Baracchi <lorenzo.baracchi@usi.ch>, Ebrisa Savina
 *         Mastrodicasa
 * 
 */
public class PasteActionListener implements ActionListener
{
    private JSpreadSheetGUI spreadTable;

    public PasteActionListener(JSpreadSheetGUI spreadTable)
    {
	   this.spreadTable = spreadTable;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
		  MultiplePaste paste;
		  paste = new MultiplePaste(spreadTable.getSheet(), spreadTable
				.getSelectedRows(), spreadTable.getSelectedColumns(),
				(MultipleTextManagement) spreadTable.getStack().getLastAction());
		  spreadTable.getStack().addActionToUndoStack(paste);
		  paste.evaluate();
		  SpreadSheetTableModel tb = (SpreadSheetTableModel) spreadTable
				.getTable().getModel();
		  tb.fireTableCellUpdated(spreadTable.getSelectedRow() + 1,
				spreadTable.getSelectedColumn() + 1);
		  spreadTable.getTable().repaint();
		  spreadTable.getSheet().setHasBeenSaved(false);
    }

}
